﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBHelper;
using BusinessLayer.RegInfo.Entities;
using System.Data.SqlClient;
using System.Data;
using BusinessLayer.RegInfo.Models;


namespace BusinessLayer.RegInfo
{
    public class AdminUserBL
    {
        private readonly DbHelper _db;

        public AdminUserBL()
        {
            _db = new DbHelper(new DbConfig().DbRegInfoConnectionString);
        }

        public List<AdminUserExtend> GetAll()
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAllAdminUsers) { CommandType = CommandType.StoredProcedure };

            var list = _db.GetList<AdminUserExtend>(oCommand);
            return list;
        }
        public int CreateAdminUser(AdminUser user)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.InsertAdminUser) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserName", user.UserName));
                oCommand.Parameters.Add(new SqlParameter("@Password", user.Password));
                oCommand.Parameters.Add(new SqlParameter("@Status", user.Status));
                oCommand.Parameters.Add(new SqlParameter("@FirstName", user.FirstName));
                oCommand.Parameters.Add(new SqlParameter("@LastName", user.LastName));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
        public int UpdateAdminUser(AdminUser user)
        {
            int result = -1;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.UpdateAdminUser) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserID", user.UserID));
                oCommand.Parameters.Add(new SqlParameter("@UserName", user.UserName));
               
                oCommand.Parameters.Add(new SqlParameter("@Status", user.Status));
                oCommand.Parameters.Add(new SqlParameter("@FirstName", user.FirstName));
                oCommand.Parameters.Add(new SqlParameter("@LastName", user.LastName));

                result = _db.ExecuteNonQuery(oCommand);
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public AdminUser GetByUserName(string username)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserByUserName) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserName", username));
            var list = _db.GetList<AdminUser>(oCommand);
            return list.FirstOrDefault();
        }
        public AdminUser GetByUserID(int id)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserByID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserID", id));
            var list = _db.GetList<AdminUser>(oCommand);
            return list.FirstOrDefault();
        }
        public int DeleteAdminUserByIds(List<string> lstofids)
        {
            int result = -1;

            string ids = "";
            foreach (var id in lstofids)
            {
                ids += id + ",";
            }
            ids = String.IsNullOrEmpty(ids) ? "" : ids.Substring(0,ids.Length-1);
            if (!String.IsNullOrEmpty(ids))
            {
                try
                {
                    var oCommand = new SqlCommand(String.Format("DELETE FROM AdminUsers WHERE UserID IN ({0});", ids)) { CommandType = CommandType.Text };
                    result = _db.ExecuteNonQuery(oCommand);
                    return result;
                }
                catch (Exception ex)
                {

                }
            }
            return result;
        }
        //GetAdminUserByUserGroupID
        public List<AdminUser> GetAdminUsersByUserGroupID(int usergroupid)
        {
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserByUserGroupID) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserGroupID", usergroupid));
            var list = _db.GetList<AdminUser>(oCommand);
            return list;
        }
        public bool ValidateUser(string username, string password)
        {
            bool result = false;
            var oCommand = new SqlCommand(Common.Constants.StoredProcedures.GetAdminUserByUserNameAndPassword) { CommandType = CommandType.StoredProcedure };
            oCommand.Parameters.Add(new SqlParameter("@UserName", username));
            oCommand.Parameters.Add(new SqlParameter("@Password", password));
            var list = _db.GetList<AdminUser>(oCommand);
            result = list.FirstOrDefault()!=null;
            return result;
        }
        public bool ChangePassword(int userid,string oldPassword, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ChangePasswordAdminUser) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserID", userid));

                oCommand.Parameters.Add(new SqlParameter("@Password", oldPassword));
                oCommand.Parameters.Add(new SqlParameter("@NewPassword", newPassword));
               

                result = _db.ExecuteNonQuery(oCommand)>0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public bool ResetPassword(int userid, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ResetPasswordAdminUser) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserID", userid));
                oCommand.Parameters.Add(new SqlParameter("@NewPassword", newPassword));


                result = _db.ExecuteNonQuery(oCommand) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public bool ChangePasswordByUserName(string username, string oldPassword, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ChangePasswordAdminUserByUserName) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserName", username));

                oCommand.Parameters.Add(new SqlParameter("@Password", oldPassword));
                oCommand.Parameters.Add(new SqlParameter("@NewPassword", newPassword));


                result = _db.ExecuteNonQuery(oCommand) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }

        public bool ResetPasswordByUserName(string username, string newPassword)
        {
            bool result = false;
            try
            {
                var oCommand = new SqlCommand(Common.Constants.StoredProcedures.ResetPasswordAdminUserByUserName) { CommandType = CommandType.StoredProcedure };
                oCommand.Parameters.Add(new SqlParameter("@UserName", username));
                oCommand.Parameters.Add(new SqlParameter("@NewPassword", newPassword));


                result = _db.ExecuteNonQuery(oCommand) > 0;
                return result;
            }
            catch (Exception ex)
            {

            }
            return result;


        }
    }
}
